<template>
  <el-dialog
    title="分配角色"
    :visible="showRoleDialog"
    append-to-body
    @close="hClose"
  >
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    {{ curId }}

    <el-row>
      <el-button type="primary" size="small" @click="hSave">确定</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRolesEnabled, assignRoles } from '@/api/role'
import { getEmployeeDetail } from '@/api/employee'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      required: true
    },
    curId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      // 所有的角色数据roleList
      roleList: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async hSave() {
      // 调用接口
      await assignRoles({ id: this.curId, roleIds: this.roleIds })
      this.$message.success('保存成功')
      this.hClose()
    },
    async getRolesById() {
      const res = await getEmployeeDetail(this.curId)
      //   回填
      this.roleIds = res.data.roleIds
    },
    hClose() {
      // 关闭弹框
      this.$emit('update:showRoleDialog', false)
    },
    async loadRoles() {
      const res = await getRolesEnabled()
      console.log(res)
      this.roleList = res.data
    }
  }
}
</script>
